/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "frmLogin.h"
#include "frmChangePassword.h"
#include "ui_frmChangePassword.h"
#include <QMessageBox>
#include <QPushButton>

frmChangePassword::frmChangePassword(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmChangePassword)
{
    setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
#ifdef MACX
    this->setMinimumSize(325,250);
    this->setMaximumSize(325,250);
    this->resize(325,250);
#else
#ifdef UNIX
    this->setMinimumSize(325,250);
    this->setMaximumSize(325,250);
    this->resize(325,250);
#endif
#endif
    QPushButton *changeButton = ui->bbOptions->button(QDialogButtonBox::Apply);
    changeButton->setText(QApplication::tr("Change"));
}

frmChangePassword::~frmChangePassword()
{
    delete ui;
}

void frmChangePassword::setForm(frmLogin* login)
{
    form = login;
}

void frmChangePassword::on_cmdChange_clicked()
{
    frmLogin *login = (frmLogin*)form;
    if (login->isConnectedWithServer())
    {
        QString npassword = ui->txtNewPassword->text();
        QString opassword = ui->txtOldPassword->text();
        if (npassword != ui->txtRepeatPassword->text()) {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The passwords don't match!")); return;}
        if (npassword.length() >= 8)
        {
            if (npassword.trimmed() == npassword)
            {
                if (npassword.contains("\n") || npassword.contains("\r"))
                {
                    {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The new password contains CR or LF in binary form!")); return;}
                    return;
                }
            }
            else
            {
                {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The new password is not the same in trimmed and not trimmed form!")); return;}
                return;
            }
            if (QString::fromUtf8(npassword.toUtf8()) != QString::fromLatin1(npassword.toUtf8()))
            {
                {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The new password is not \"UTF-8 without BOM\" compatible!")); return;}
                return;
            }
            if (QString::fromUtf8(npassword.toUtf8()) != QString::fromUtf8(npassword.toLatin1()))
            {
                {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The new password is not \"UTF-8 without BOM\" compatible!")); return;}
                return;
            }
        }
        else
        {
            {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The new password is shorter than %1 bytes!").arg("8")); return;}
            return;
        }
        int ret = login->setUserPassword(opassword.trimmed(),npassword.trimmed());
        if (ret == 200)
        {
            this->close();
        }
        else if (ret == 300)
        {
            {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The old password is not correct!")); return;}
            return;
        }
        else
        {
            {QMessageBox::warning(this,QApplication::tr("Change password"), QApplication::tr("The server return error code %1!").arg(QString::number(ret))); return;}
            return;
        }
    }
    else
    {
        QMessageBox::information(this,QApplication::tr("Offline"),QApplication::tr("You are offline, please wait until you're online!"));
    }
}

void frmChangePassword::on_cmdAbort_clicked()
{
    this->close();
}

void frmChangePassword::on_bbOptions_clicked(QAbstractButton *button)
{
    if (ui->bbOptions->buttonRole(button) == QDialogButtonBox::ApplyRole)
    {
        on_cmdChange_clicked();
    }
    else if (ui->bbOptions->buttonRole(button) == QDialogButtonBox::RejectRole)
    {
        on_cmdAbort_clicked();
    }
}
